package com.kongjian.manage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.kongjian.manage.common.R;
import com.kongjian.manage.service.IUserService;
import com.kongjian.manage.model.vo.UserVO;
import com.kongjian.manage.model.dto.UpdateUserDTO;
import com.kongjian.manage.model.dto.NewUserDTO;
import com.kongjian.manage.model.query.UserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author tangjunfeng
 * @Classname UserController.java
 * @Description 用户controller
 * @Date 2023-10-09
 * @Version V1.0
 */
@Api(tags = "用户")
@RestController
@Validated
@RequestMapping("/api/v1/user")
public class UserController {


    @Autowired
    private IUserService userService;

    @ApiOperationSupport(order = 1)
    @GetMapping("page")
    @ApiOperation("查询")
    public R<Page> queryPageUser(@ApiIgnore UserQuery query) throws Exception{
        return userService.queryPageUser(query);
    }

    @ApiOperationSupport(order = 2)
    @GetMapping("count")
    @ApiOperation("统计")
    public Integer countUser(UserQuery query) {
        return userService.countUser(query);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("根据ID查询")
    @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", example = "1", required = true)
    @GetMapping("/{id}")
    public UserVO getByIdUser(@PathVariable("id") Long id) throws Exception {
        return userService.getByIdUser(id);
    }

    @ApiOperation(value = "新增")
    @PostMapping("")
    @ApiOperationSupport(order = 4)
    public R<String> addUser(@RequestBody @Validated NewUserDTO userDTO) throws Exception {
       userService.addUser(userDTO);
       return R.success("新增成功");
    }

    @ApiOperation("更新")
    @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", example = "1", required = true)
    @PutMapping("/{id}")
    @ApiOperationSupport(order = 5)
    public R<String> modifyUser(@PathVariable("id") Long id, @Validated @RequestBody UpdateUserDTO userDTO) throws Exception {
        userService.modifyUser(id, userDTO);
        return R.success("更新成功");
    }

    @ApiOperation("删除")
    @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", example = "1", required = true)
    @DeleteMapping("/{id}")
    @ApiOperationSupport(order = 6)
    public R<String> deleteUser(@PathVariable("id") Long id) throws Exception {
        userService.deleteUser(id);
        return R.success("删除成功");
    }



}
